home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2236 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can't figure this out
  5. Date: 19 Jan 1996 15:25 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <19JAN199615253245@erich.triumf.ca>
  9. References: <31000091.3778302@news.panix.com>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <31000091.3778302@news.panix.com>, dm@panix.com (Dan'l) writes...
  14. >I am learning C and I have not had any problems understanding most
  15. >concepts I have learned so far.  But to date I still can't figure out
  16. >how the outcome of this program is 15.  Somehow one of the B's ends up
  17. >a three and the other B a 5, or am I so off base that I can't see
  18. >what's really happening.    Can someone please walk me through this
  19. >one.                                             Thanks     Dan'l
  20. >#define A 3
  21. >#define B A + A
  22.  
  23. This makes B = "3 + 3", not 6 - #defines just do text substitution, not
  24. arithmetic.
  25.  
  26. >#define C B * B
  27.  
  28. Now C = 3 + 3 * 3 + 3.  The multiplication will be done first, so you get 
  29. 3 + 9  + 3 = 15
  30.  
  31. If you _really_ must do something like this, enclose the expressions in
  32. parenthesis, like so:
  33. #define A 3
  34. #define B ((A) + (A))
  35. #define C ((B) * (B))
  36.  
  37. I think I have enough parentheses  there to ensure things are evaluated in the
  38. right order...  :-)
  39.  
  40.  
  41. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  42. Internet: bennett@triumf.ca         | of one another only when one can be
  43. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  44. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  45. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  46.  
  47.